<?php

abstract class Image {


	protected $id_user;
	protected $extension;
	protected $mime;
	protected $file;
	protected $time;
	protected $id_image;
   
	protected function __construct($map) {
		
		$this->id_image = isset($map['id_image']) ? $map['id_image'] : md5(microtime()); 
		
		if (isset($map['id_user'])){
			$this->id_user = $map['id_user'];
		} else {
			$this->id_user = $_SESSION['infosAuth']->getIdUser();
		}
		
		if (isset($map['time'])){
			$this->time= $map['time'];
		} else {
			$this->time = date("Y-m-d H:i:s");
		}
		
	}
	
	 public function update($updateData) {
	  /* données à changer */
	
	}
  
	public static function initializeFromData($map=array()) {
	  
		switch($map['mime']) {
			case "image/jpeg":
				return new Image_Jpg($map);
				break;
		  
			case "image/png":
				return new Image_Png($map);
				break;
				
			default:
				throw new Exception("Erreur de la photo venant de la base de donnée");
				break;
		}
  
	}
  
	public static function initializeFromFile($file) {
	  	return self::factory($file);
	}
  
	protected static function factory($file) {
		if (is_file($file)) {
		 	 $mime=@exec("file --mime-type -b {$file}");
		} else {
		 	 throw new Exception("le fichier {$file} n'existe pas");
		}
		switch($mime) {
			case "image/jpeg":
			  	return new Image_Jpg();
			  	break;
		
			case "image/png":
			  	return new Image_Png();
			  	break;
		
			default:
			  	return false;
			  	break;
		}
  
	}
  
	public function redimensionner($orig, $dest, $newLargeur, $maxHauteur, $mime) {
	   switch ($mime) {
	  case "image/jpeg" :
		$type = "jpeg";
		break;
	  case "image/png" :
		$type = "png";
		break;
	  default :
		throw new Exception("Impossible de redimensionner les fichiers de type {$mime}");
		break;
	  }
  
	  $fonction = "imagecreatefrom" . $type;
	  $image = $fonction($orig); 
	  
	  $largeurImage = imagesx($image);
	  if ($largeurImage < $newLargeur) {
		if (!copy($orig, $dest)) {
	  throw new Exception("Impossible de copier le fichier {$orig} vers {$dest}");
		}
	  } else {
		$hauteurImage = imagesy($image);   
		$newHauteur = (int)(($newLargeur*$hauteurImage) / $largeurImage );       
		if ($newHauteur > $maxHauteur) {
	  $newHauteur = $maxHauteur;
	  $newLargeur = (int) (($newHauteur * $largeurImage) / $hauteurImage);
		}
		//      echo $newHauteur . "-" . $newLargeur;
		$newImage  = imagecreatetruecolor($newLargeur, $newHauteur);
		imagecopyresampled ($newImage, $image, 0, 0, 0, 0, $newLargeur, $newHauteur, $largeurImage, $hauteurImage);     
  
		$fonction= "image" . $type;
		$fonction($newImage,$dest);
  
		imagedestroy($newImage);
		imagedestroy($image);
	  }
	}
  
	public function redimensionnerCarre($orig, $dest, $objectifLargeur, $mime) {
	   switch ($mime) {
	  case "image/jpeg" :
		$type = "jpeg";
		break;
	  case "image/png" :
		$type = "png";
		break;
	  default :
		throw new Exception("Impossible de redimensionner les fichiers de type {$mime}");
		break;
	  }
  
	  $fonction = "imagecreatefrom" . $type;
	  $image = $fonction($orig); 
	  
	  $largeurImage = imagesx($image);
	  $hauteurImage = imagesy($image);
	  if ($hauteurImage > $largeurImage) {
		$newLargeur = $objectifLargeur;
		$newHauteur = (int)(($newLargeur*$hauteurImage) / $largeurImage );
	  } else {
		$newHauteur = $objectifLargeur;
		$newLargeur = (int) (($newHauteur * $largeurImage) / $hauteurImage);
	  }
	  $newImage  = imagecreatetruecolor($newLargeur, $newHauteur);
  
	  imagecopyresampled ($newImage, $image, 0, 0, 0, 0, $newLargeur, $newHauteur, $largeurImage, $hauteurImage);
  
	  $squareImage  = imagecreatetruecolor($objectifLargeur, $objectifLargeur);
	  
	  if ($newHauteur > $newLargeur) {
		$left = 0;
		$top = (int) ($newHauteur - $objectifLargeur) / 2;
	  } else {
		$top = 0;
		$left = (int) ($newLargeur - $objectifLargeur) / 2;
	  }
  
	  imagecopyresized($squareImage, $newImage, 0, 0, $left, $top, $objectifLargeur, $objectifLargeur, $objectifLargeur, $objectifLargeur);
  
	  $fonction= "image" .$type;
	  $fonction($squareImage,$dest); 
	  imagedestroy($newImage);
	  imagedestroy($squareImage);
	  imagedestroy($image);
	}
	
	protected function imageCreateFromFormat($fileSrc,$mime){
	  switch ($mime){
		case "image/jpeg":
		  return imagecreatefromjpeg($fileSrc);
		break;
		case "image/gif":
		  return imagecreatefromgif($fileSrc);
		break;
		case "image/png":
		  return imagecreatefrompng($fileSrc);
		break;
	  }
	}
	
	protected function imageFormat($gdImg,$imgDest,$mime){
	  switch ($mime) { 
		case "image/jpeg":
		  imagejpeg ($gdImg, $imgDest);
		break;
		case "image/png":
		  imagepng ($gdImg, $imgDest);
		break;
		case "image/gif":
		  imagegif ($gdImg, $imgDest);
		break;
	  }
	}
	
	public function resizeScale_MIN($source, $dest, $t_w, $t_h, $fill_callback=NULL, $mime){
		  #
		  # source dimensions
		  #
			   switch ($mime) {
	  case "image/jpeg" :
		$type = "jpeg";
		break;
	  case "image/png" :
		$type = "png";
		break;
	  default :
		throw new Exception("Impossible de redimensionner les fichiers de type {$mime}");
		break;
	  }
		  
		  $fonction = "imagecreatefrom" . $type;
		   $image = $fonction($source); 
		  
		  
		  $s_x = 0;
		  $s_y = 0;
		  $s_w = imagesx($image);
		  $s_h = imagesy($image);
		  
		  #
		  # destination dimensions
		  #
  
		  $d_x = 0;
		  $d_y = 0;
		  $d_w = $t_w;
		  $d_h = $t_h;
		  
		  // SCALE MIN
		  
		  $s_r = $s_w / $s_h;
		  $d_r = $d_w / $d_h;
				  
		  if ($s_r > $d_r) {
			  $r = $s_h / $d_h;
			  $s_x += round(($s_w - $t_w * $r) / 2);
		  } else {
			  $r = $s_w / $d_w;                   
			  $s_y += round(($s_h - $t_h * $r) / 2);
		  }     
		  $d_w = round($s_w / $r);
		  $d_h = round($s_h / $r);
		  
		   #
		  # center destination image result
		  #
		  
		  if ($t_h > $d_h)
		  {
			  $d_y = round(($t_h - $d_h) / 2);
		  }
		  
		  if ($t_w > $d_w)
		  {
			  $d_x = round(($t_w - $d_w) / 2);
		  }
		  
		  #
		  # create destination image
		  #
		  
		  $destination = imagecreatetruecolor($t_w, $t_h);
		  if ($type == "png") {
		   imagealphablending($destination, false);
		   imagesavealpha($destination,true);
		  }
		  #
		  # call user's function to fill the area
		  #
		  
		  if ($fill_callback)
		  {
			  call_user_func($fill_callback, $destination, $t_w, $t_h);
		  }
  
		  #
		  # now we resize and paste our image
		  #
		  
		  imagecopyresampled($destination,$image,$d_x, $d_y, $s_x, $s_y,$d_w, $d_h, $s_w, $s_h);
		  
		  $fonction= "image" . $type;
		  $qualite = $type == "jpeg" ? 90 : "";
		  $fonction($destination,$dest,$qualite);
		  imagedestroy($image);
		  imagedestroy($destination);
  
	}
  
  
	public function creerIcone() {
  
		$orig = ENTREPOT_FILE . $this->getIdUser() . "/" . $this->getIdImage() . "_original." . $this->getExtension();
		$vignette = ENTREPOT_FILE . $this->getIdUser() . "/" . $this->getIdImage() . "_vignette." . $this->getExtension();
		$minivignette = ENTREPOT_FILE . $this->getIdUser() . "/" . $this->getIdImage(). "_minivignette." . $this->getExtension();
		$picovignette = ENTREPOT_FILE . $this->getIdUser() . "/" . $this->getIdImage(). "_picovignette." . $this->getExtension();
	
		$this->resizeScale_MIN($orig, $vignette, 100, 100, NULL, $this->getMime());
		$this->resizeScale_MIN($orig, $minivignette, 45, 45, NULL, $this->getMime());
		$this->resizeScale_MIN($orig, $picovignette, 15, 15, NULL, $this->getMime());
	}
  
	public function getVignette(){
	  	return ENTREPOT_URL . $this->getIdUser() . "/" . $this->getIdImage() . "_vignette." . $this->getExtension();
	}
	
	public function getMiniVignette(){
		return 	ENTREPOT_URL . $this->getIdUser() . "/" . $this->getIdImage(). "_minivignette." . $this->getExtension();
	}
	
	public function getPicoVignette(){
		return 	ENTREPOT_URL . $this->getIdUser() . "/" . $this->getIdImage(). "_picovignette." . $this->getExtension();
	}
	
	  // Accesseurs
	  
	public function getIdImage(){
		return $this->id_image;	
	}
	
	public function getIdUser() {
	  	return $this->id_user;
	}
	
	public function getTime() {
	  	return $this->time;
	}
  
	public abstract function getMime();
  
	public abstract function getExtension();
	
	public abstract function getFile();
	
	// Mutateurs
	
	public function setIdUser($id){
	  	$this->id_user = $id;  
	}
	
	public static function supprimerRepImage($id_user) {
		$dir = ENTREPOT_FILE . $id_user;
   		if (is_dir($dir)) {
     		$objects = scandir($dir);
     		foreach ($objects as $object) {
       			if ($object != "." && $object != "..") {
        			if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
       		}
     	}
    	reset($objects);
     	rmdir($dir);
   		}  
	}
  
} // fin classe Image

?>